package com.greateamd.timetableasy.client.mvc.view.crud.cursus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.services.cursus.CursusService;
import com.greateamd.timetableasy.client.services.cursus.CursusServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.studyperiod.StudyPeriodBuilder;
import com.greateamd.timetableasy.client.widgets.university.UniversityListBox;
import com.greateamd.timetableasy.domain.Cursus;
import com.greateamd.timetableasy.domain.StudyPeriod;
import com.greateamd.timetableasy.domain.User;

public class AddCursusPage <M extends Model> extends IAddCursusPage<M>{

	private final CursusServiceAsync cursusService = GWT.create(CursusService.class);

	@UiTemplate("AddCursusPage.ui.xml")
	interface AddCursusPageUIBinder extends UiBinder<Widget, AddCursusPage> {}
	private static AddCursusPageUIBinder uiBinder = GWT.create(AddCursusPageUIBinder.class);
	
	@UiField TextBox name;
	@UiField UniversityListBox universitiesLb;
	@UiField DateBox dateStart;
	@UiField DateBox dateEnd;
	//Study period 
	@UiField StudyPeriodBuilder studyPeriod;
	@UiField Button submitBt;
	
	/**
	 * Constructor
	 */
	public AddCursusPage() {
	    initWidget(uiBinder.createAndBindUi(this));
	}
	
	public Widget getWidgetPage() {
		return this;
	}

	@Override
	public void setUser(User user) {
	}


	//---------------------------------------------------------------------
	//-------------------------- UI action

	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	@UiHandler("submitBt")
	void onSubmitBtClick(ClickEvent e){
		addCursus();
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private void addCursus() {
		if(!checkFormValidity()){
			Window.alert("Invalid form");
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				Cursus toPersist = new Cursus(name.getText(),dateStart.getValue(),dateEnd.getValue(),universitiesLb.getSelectedUniversity());
				for(StudyPeriod sp : studyPeriod.getListOfStudies()){
					sp.setCursus(toPersist);
					toPersist.addStudyPeriod(sp);
				}
				cursusService.save(toPersist,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				Window.alert("Success " +success);
				//TODO: Could return to a menu
			}
		}.call();
	}

	private boolean checkFormValidity() {
		if(name.getText()=="") return false;
		return true;
	}
}
